{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[0, 0, 0, 2, 2],\n",
       "        [0, 0, 0, 3, 3],\n",
       "        [0, 0, 0, 1, 1],\n",
       "        [1, 1, 1, 0, 0],\n",
       "        [2, 2, 2, 0, 0],\n",
       "        [5, 5, 5, 0, 0],\n",
       "        [1, 1, 1, 0, 0]])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "def load_data():\n",
    "    A = np.mat([[0, 0, 0, 2, 2], [0, 0, 0, 3, 3], [0, 0, 0, 1, 1],\n",
    "                [1, 1, 1, 0, 0], [2, 2, 2, 0, 0], [5, 5, 5, 0, 0],\n",
    "                [1, 1, 1, 0, 0]])\n",
    "\n",
    "    return A\n",
    "\n",
    "\n",
    "A = load_data()\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((7, 7), (7, 5), (5, 5))"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#奇异值分解\n",
    "U, temp, VT = np.linalg.svd(A)\n",
    "\n",
    "sigma = np.zeros(A.shape)\n",
    "for i in range(len(temp)):\n",
    "    sigma[i, i] = temp[i]\n",
    "\n",
    "U.shape, sigma.shape, VT.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.,  0.,  0.,  0., -0., -0., -0.],\n",
       "       [ 0.,  1.,  0., -0.,  0., -0.,  0.],\n",
       "       [ 0.,  0.,  1.,  0., -0., -0., -0.],\n",
       "       [ 0., -0.,  0.,  1., -0.,  0.,  0.],\n",
       "       [-0.,  0., -0., -0.,  1., -0., -0.],\n",
       "       [-0., -0., -0.,  0., -0.,  1., -0.],\n",
       "       [-0.,  0., -0.,  0., -0., -0.,  1.]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#U * U.T = E\n",
    "np.around(U * U.T, decimals=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.,  0.,  0., -0.,  0.],\n",
       "       [ 0.,  1.,  0.,  0., -0.],\n",
       "       [ 0.,  0.,  1., -0.,  0.],\n",
       "       [-0.,  0., -0.,  1.,  0.],\n",
       "       [ 0., -0.,  0.,  0.,  1.]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#V * V.T = E\n",
    "np.around(VT * VT.T, decimals=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0., -0., -0.,  2.,  2.],\n",
       "       [ 0., -0., -0.,  3.,  3.],\n",
       "       [ 0., -0., -0.,  1.,  1.],\n",
       "       [ 1.,  1.,  1.,  0.,  0.],\n",
       "       [ 2.,  2.,  2., -0., -0.],\n",
       "       [ 5.,  5.,  5.,  0.,  0.],\n",
       "       [ 1.,  1.,  1., -0., -0.]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#U * sigma * V = A\n",
    "np.around(U * sigma * VT, decimals=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#求保留90%信息量的切分点\n",
    "def get_k():\n",
    "    sigma_power_sum = np.power(sigma, 2).sum()\n",
    "\n",
    "    _sum = 0\n",
    "    for i in range(min(sigma.shape)):\n",
    "        _sum += np.power(sigma[i, i], 2)\n",
    "\n",
    "        #保留90%的信息量\n",
    "        if _sum >= sigma_power_sum * 0.9:\n",
    "            return i\n",
    "\n",
    "\n",
    "k = get_k()\n",
    "k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((7, 1), (1, 1), (1, 5))"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#矩阵降秩\n",
    "U = U[:, :k]\n",
    "sigma = sigma[:k, :k]\n",
    "VT = VT[:k, :]\n",
    "\n",
    "U.shape, sigma.shape, VT.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0., -0., -0.,  0.,  0.],\n",
       "       [-0., -0., -0.,  0.,  0.],\n",
       "       [-0., -0., -0.,  0.,  0.],\n",
       "       [ 1.,  1.,  1., -0., -0.],\n",
       "       [ 2.,  2.,  2., -0., -0.],\n",
       "       [ 5.,  5.,  5., -0., -0.],\n",
       "       [ 1.,  1.,  1., -0., -0.]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#矩阵降秩\n",
    "A2 = U * sigma * VT\n",
    "\n",
    "np.around(A2, decimals=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 1)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#计算矩阵的秩\n",
    "np.linalg.matrix_rank(A), np.linalg.matrix_rank(A2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
